<?php

namespace App\Modules\Admin\Entities\Mall;

use App\Models\Model;
use App\Modules\Admin\Entities\Member\UsersModel;
use PhpParser\Node\Expr\AssignOp\Mod;

class GoodsOrderModel extends Model
{

    protected $table = 'goods_order';
    protected $primaryKey = 'order_id';
    protected $is_delete=0;
    public $timestamps = false;
    // 支付状态
    public $pay_status = [
        1 => '待支付',
        2 => '已支付',
        3 => '已退款',
        4 => '部分退款',
        5 => '部分支付',
        6 => '支付异常'
    ];

    // 支付方式
    public $pay_way = [
        1 => '微信',
        2 => '支付宝',
        3 => '余额'
    ];

    // 订单状态
    public $order_status = [
        1 => '待确认',
        2 => '待支付',
        3 => '待发货',
        4 => '待收货',
        5 => '部分发货',
        6 => '交易完成',
        7 => '已取消',
        8 => '已关闭',
        9 => '库存不足'
    ];

    // 订单类型
    public $order_type = [
        1 => '普通订单',
        2 => '拼团订单',
        3 => '秒杀订单',
        4 => '砍价订单'
    ];

    // 退款订单状态
    public $refund_status = [
        1 => '退款申请中',
        2 => '退款审批中',
        3 => '已退款'
    ];

    // 售后订单状态
    public $refund_order_status = [
        1 => '待审批',
        2 => '已退款',
        3 => '已拒绝',
        4 => '取消'
    ];


    //修改支付状态
    public function updateOrderPayStatus($orderId){
        return $this->where(['order_id'=>$orderId])->update(['pay_status'=>2,'pay_time'=>time()]);
    }
    //修改订单状态
    public function updateOrderStatus($orderId,$status){
        return $this->where(['order_id'=>$orderId])->update(['status'=>$status]);
    }

    public function detail_info()
    {
        return $this->hasMany(GoodsOrderDetailModel::class, 'order_id', 'order_id')->select(['*']);
    }

    public function user_info()
    {
        return $this->hasOne(UsersModel::class, 'id', 'user_id');
    }

    public function addressInfo()
    {
        return $this->hasOne(GoodsOrderAddressModel::class, 'order_id', 'id');
    }

    public function refundInfo()
    {
        return $this->hasOne(GoodsOrderRefundModel::class, 'order_id', 'id')->whereIn('status', [1, 2])->select(['status']);
    }

}
